#include "CRectPickColor.h"
#include <QMouseEvent>
#include <QDebug>

CRectPickColor::CRectPickColor(QWidget *parent) : QWidget(parent)
{

}

void CRectPickColor::setPickRowCol(int nRowCnt, int nColCnt)
{
    m_nMaxRow = nRowCnt;
    m_nMaxCol = nColCnt;
    update();
}

void CRectPickColor::setCellRadius(int nCellRadius)
{
    m_nCellRadius = nCellRadius;
    update();
}

void CRectPickColor::setCellMargins(int nAllMargin)
{
    m_marginCell = QMargins(nAllMargin,nAllMargin,nAllMargin,nAllMargin);
    update();
}

void CRectPickColor::setCellMargins(QMargins margins)
{
    m_marginCell = margins;
    update();
}

void CRectPickColor::setColor(QColor color)
{
    m_colorSelected = color;
    m_ptClicked = QPoint(-1,-1);
    update();
}

void CRectPickColor::mousePressEvent(QMouseEvent *event)
{
    m_ptClicked = event->pos();
    update();
}

void CRectPickColor::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);

    int nCellWidth = width()/m_nMaxCol;
    int nCellHeight = height()/m_nMaxRow;

    int nCnt = 0; //格子序号  （格子序号÷格子总数）*255 = 格子颜色亮度（0~255）
    QColor colorTmp = m_colorSelected;

    //左上部分
    for(int nCol = 0; nCol <  m_nMaxCol; nCol++)
    {
        for(int nRow = 0; nRow <= nCol && nRow < m_nMaxRow; nRow++)
        {
            //计算格子位置
            QRect rect((nCol-nRow)*nCellWidth,nRow*nCellHeight,nCellWidth,nCellHeight);

            drawCell(painter,rect,(1.0 - nCnt/(double)(m_nMaxCol*m_nMaxRow)));
            nCnt++;
        }
    }


    //右下部分
    for(int nRow = 1; nRow < m_nMaxRow; nRow++)
    {
        int nRowOffset = 0;
        for(int nCol = m_nMaxCol-1; nCol >= 0; nCol--)
        {
            if(nRow+nRowOffset >= m_nMaxRow)
                break ;
            QRect rect((nCol)*nCellWidth,(nRow+nRowOffset)*nCellHeight,nCellWidth,nCellHeight);
            drawCell(painter,rect,(1.0 - nCnt/(double)(m_nMaxCol*m_nMaxRow)));

            nCnt++;
            nRowOffset++;
        }
    }
}

void CRectPickColor::drawCell(QPainter &painter, QRect &rect, qreal dCellLightness)
{
    QColor colorTmp = m_colorSelected;

    //选中格子边框设置
    if(rect.contains(m_ptClicked)){
        painter.setPen(QPen(colorTmp.darker(),3,Qt::DashLine));
        if(m_colorSelected != colorTmp)
            m_colorSelected = colorTmp;
    }else{
        painter.setPen(Qt::NoPen);
    }


    //格子填充色设置
    colorTmp.setHsl(colorTmp.hslHue(),colorTmp.hslSaturation(),dCellLightness*255);
    painter.setBrush(QBrush(colorTmp));
    rect.adjust(m_marginCell.left(),m_marginCell.top(),-m_marginCell.right(),-m_marginCell.bottom()); //格子外边距

    painter.drawRoundedRect(rect,m_nCellRadius,m_nCellRadius);

    //显示调试信息
//    painter.setPen(Qt::red);
//    painter.drawText(rect,QString("cnt:%1").arg((int)((1.0-dCellLightness)*m_nMaxCol*m_nMaxRow)));
    qDebug()<<"------"<<QString("cnt:%1 row:%2,col:%3").arg((int)((1.0-dCellLightness)*m_nMaxCol*m_nMaxRow)).arg(rect.top()/rect.height()).arg(rect.left()/rect.width());
}
